package com.liao.controller;

import com.liao.bean.Money;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.service.MoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/money")
public class MoneyController {

    @Autowired
    private MoneyService moneyService;

    @ResponseBody
    @RequestMapping("/queryMoneyListByPage")
    public PageResult<Money> queryMoneyListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "5") Integer pageSize,
                                               @RequestParam(defaultValue = "") String userId) throws Exception {
        PageResult<Money> pageResult = moneyService.queryMoneyListByPage(currentPage,pageSize,userId);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("/addMoney")
    public Result addMoney(@RequestBody Money money) throws Exception {

        moneyService.addMoney(money);
        return new Result(true, Code.MEMBER_ADD_SUCCESS, Msg.MEMBER_ADD_SUCCESS);

    }

    @ResponseBody
    @RequestMapping("/editMoney")
    public Result editMoney( @RequestBody Money money) throws Exception {
        moneyService.editMoney(money);
        return new Result(true,Code.MEMBER_EDIT_SUCCESS, Msg.MEMBER_EDIT_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteMoney")
    public Result deleteMoney(String umId) throws Exception {
        moneyService.deleteMoney(umId);
        return new Result(true,Code.MEMBER_DELETE_SUCCESS, Msg.MEMBER_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteMoneyIds")
    public Result deleteMoneyIds(@RequestParam("umIds[]") List<String> umIds) throws Exception {
        for (String umId : umIds) {
            try {
                moneyService.deleteMoney(umId);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.MEMBER_DELETE_FAIL, Msg.MEMBER_DELETE_FAIL);
            }

        }
        return new Result(true,Code.MEMBER_DELETE_SUCCESS, Msg.MEMBER_DELETE_SUCCESS);
    }
}
